/**
 * Predefined variables
 * Name = AccountLayer
 * DateTime = Sun Jan 16 2022 22:37:04 GMT+0800 (中国标准时间)
 * Author = Created by zhj9922
 * FileBasename = AccountLayer.ts
 * gitee： https://gitee.com/coyeking
 */
 
import { _decorator, sys, director,EditBox,Toggle } from 'cc';
const { ccclass, property } = _decorator;
import { UiBase } from '../uiBase';
import { audioManager } from '../../framework/audioManager';
import { uiManager } from '../../framework/uiManager';
import { verify } from '../../utils/verify';
import { ModeluType } from '../../define/ccenum';
import { httpApi } from '../../api/httpApi';
import { userGlobal } from '../../global/userGlobal';
import { constant } from '../../define/constant';
 
@ccclass('AccountLayer')
export class AccountLayer extends UiBase {
    @property({
        type: Toggle,
        displayName:'复选块',
    })
    public ui_Toggle: Toggle = null;
    
    @property(EditBox)
    public ui_accontEdit: EditBox = null;
    
    @property(EditBox)
    public ui_pwdEdit: EditBox = null;
    
   onEnable() {
        super.onEnable();
        if (sys.localStorage.getItem(constant.LOCAL_STORAGE.user_account) && sys.localStorage.getItem(constant.LOCAL_STORAGE.user_password)){
            this.ui_accontEdit.string = sys.localStorage.getItem(constant.LOCAL_STORAGE.user_account);
            this.ui_pwdEdit.string = sys.localStorage.getItem(constant.LOCAL_STORAGE.user_password);
        }
   }

    /**
     * 按钮点击事件
     * @param event 
     * @param customEventData 
     */
	onClick (event: Event, customEventData: string) {
        audioManager.instance.playSound();

        if (customEventData == 'close'){
            this.closeAction();
        }else if(customEventData == 'account_login'){
            this.accontLogin();
        }else if(customEventData == 'account_register'){
            uiManager.createLayer('prefab/login/RegisterLayer','RegisterLayer');
            this.closeAction(null,false);
        }
    }

    // 账号登录
    accontLogin(){
        let _this = this;
        if (!this.ui_accontEdit.string || !verify.enOrNum(this.ui_accontEdit.string)){
            uiManager.showToast('账号有误,请重新输入',ModeluType.FAIL);
            return;
        };
        if (!this.ui_pwdEdit.string){
            uiManager.showToast('请输入密码',ModeluType.FAIL);
            return;
        };
        uiManager.showWaiting(true);

        httpApi.login({account:this.ui_accontEdit.string,password:this.ui_pwdEdit.string},(result)=>{
            uiManager.showWaiting(false);
            uiManager.showToast(result.msg,ModeluType.SUCCESS);
            userGlobal.instance.setUserInfo(result.data.userinfo);

            
            if (_this.ui_Toggle.isChecked){
                sys.localStorage.setItem(constant.LOCAL_STORAGE.user_account,_this.ui_accontEdit.string)
                sys.localStorage.setItem(constant.LOCAL_STORAGE.user_password,_this.ui_pwdEdit.string)
            }else{
                sys.localStorage.removeItem(constant.LOCAL_STORAGE.user_account)
                sys.localStorage.removeItem(constant.LOCAL_STORAGE.user_password)
            }

            setTimeout(function(){
                director.loadScene("hallscence");
            },1000); 
        },(res)=>{
            uiManager.showWaiting(false);
            uiManager.showToast(res.msg,ModeluType.FAIL);
        });

    }
}

